package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import project.pianpian.entity.*;
import project.pianpian.mapper.*;
import project.pianpian.utils.ReturnUtils;

import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/main")
public class SanlianAPI {

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UpvoteMapper upvoteMapper;

    @GetMapping("/collect/{id}")
    public JSONObject getcollectById(@PathVariable("collectorId") int id)
    {
        Collect collect = collectMapper.getCollectById(id);
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("collector", collect);
        return ReturnUtils.success(200,"获取收藏成功",returnJSON);

    }

    @PostMapping("/collect")
    public JSONObject addCollect(@RequestParam int VideoId, @RequestParam int collectorId)
    {

        int videoId = VideoId;
        Date time = new Date();

        Collect collect = new Collect(collectorId, videoId, time);

        int num=collectMapper.addCollect(collect);
        if(num == 0)
            return ReturnUtils.fail(500,"收藏失败！");

        return ReturnUtils.success(200,"收藏成功！");
    }

    @GetMapping("/follow/{id}")
    public JSONObject getfollowById(@PathVariable("followedId") int id)
    {
        Follow follow = followMapper.getFollowById(id);
        JSONObject returnJSON = new JSONObject();
        returnJSON.put("followed", follow);
        return ReturnUtils.success(200,"获取关注列表成功",returnJSON);

    }

    @PostMapping("/follow")
    public JSONObject addFollow(@RequestParam int FollowId, @RequestParam int followedId)
    {
        int followId = FollowId;
        Date followtime = new Date();
        Follow follow = new Follow(followId, followedId, followtime);

        int num = followMapper.addFollow(follow);
        if(num == 0)
            return ReturnUtils.fail(500,"关注失败！");

        return ReturnUtils.success(200,"关注成功！");
    }


    @PostMapping("/upvote")
    public JSONObject addUpvote(@RequestParam int VideoId, @RequestParam int upvoter)
    {
        int videoId = VideoId;
        Date time = new Date();
        Upvote upvote = new Upvote(upvoter, videoId, time);

        int num = upvoteMapper.addUpvote(upvote);
        if(num == 0)
            return ReturnUtils.fail(500,"点赞失败！");

        return ReturnUtils.success(200,"点赞成功！");
    }


}
